package com.sjgl.esdn.controller;

import com.alibaba.fastjson.JSONObject;
import com.sjgl.esdn.search.domain.MyFile;
import com.sjgl.esdn.search.domain.QueryResult;
import com.sjgl.esdn.search.domain.QueryResultTop;
import com.sjgl.esdn.search.domain.Response;
import com.sjgl.esdn.service.BlogService;
import com.sjgl.esdn.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author CSF
 */
@Api(tags = "Search模块说明")
@RestController
@CrossOrigin
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private BlogService blogService;

    @Autowired
    private FileService fileService;

    @ApiOperation("通过文件id查找文件接口")
    @GetMapping("/id/{id}")
    @CrossOrigin
    public Response getMyFileById(@ApiParam("文件的id") @PathVariable("id") String id) {
        Response response = new Response();
        try {
            MyFile myFile = blogService.getMyFileById(id);
            response.setCode(20000);
            response.setMessage("success");
            response.setResult(myFile);
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(50000);
            response.setMessage(e.getMessage());
        } finally {
            return response;
        }
    }

    @ApiOperation("通过关键词相关性查找文件的接口")
    @PostMapping("/reference")
    @CrossOrigin
    public Response searchMyFilesRef(@RequestBody JSONObject body) {
        String query = body.getString("query");
        Integer from = body.getInteger("from") == null ? 0 : body.getInteger("from");
        Integer size = body.getInteger("size") == null ? 10 : body.getInteger("size");
        Long userId = body.getLong("userId");

        Response response = new Response();

        try {
            QueryResult queryResult = blogService.getMyFilesByRef(query, from, size, userId);
            response.setCode(20000);
            response.setMessage("success");
            response.setResult(queryResult);
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(50000);
            response.setMessage(e.getMessage());
        } finally {
            return response;
        }
    }

    @ApiOperation("通过关键词相关性查找并按创建时间排序文件的接口")
    @PostMapping("/time")
    @CrossOrigin
    public Response searchMyFilesTime(@RequestBody JSONObject body) {
        String query = body.getString("query");
        Integer from = body.getInteger("from") == null ? 0 : body.getInteger("from");
        Integer size = body.getInteger("size") == null ? 10 : body.getInteger("size");
        Long userId = body.getLong("userId");

        Response response = new Response();

        try {
            QueryResult queryResult = blogService.getMyFilesByTime(query, from, size, userId);
            response.setCode(20000);
            response.setMessage("success");
            response.setResult(queryResult);
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(50000);
            response.setMessage(e.getMessage());
        } finally {
            return response;
        }
    }

    @ApiOperation("通过关键词相关性查找并按下载量排序文件的接口")
    @PostMapping("/downloadNum")
    @CrossOrigin
    public Response searchMyFilesDownloadNum(@RequestBody JSONObject body) {
        String query = body.getString("query");
        Integer from = body.getInteger("from") == null ? 0 : body.getInteger("from");
        Integer size = body.getInteger("size") == null ? 10 : body.getInteger("size");
        Long userId = body.getLong("userId");

        Response response = new Response();

        try {
            QueryResult queryResult = blogService.getMyFilesByDownload(query, from, size, userId);
            response.setCode(20000);
            response.setMessage("success");
            response.setResult(queryResult);
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(50000);
            response.setMessage(e.getMessage());
        } finally {
            return response;
        }
    }

    @ApiOperation("查找七日内下载量最高文件的接口")
    @PostMapping("/top")
    @CrossOrigin
    public Response searchMyFilesTop(@RequestBody JSONObject body) {
        Integer from = body.getInteger("from") == null ? 0 : body.getInteger("from");
        Integer size = body.getInteger("size") == null ? 10 : body.getInteger("size");
        Long userId = body.getLong("userId");

        Response response = new Response();

        try {
            QueryResultTop queryResultTop = blogService.getMyFilesByTop(from, size, userId);
            response.setCode(20000);
            response.setMessage("success");
            response.setResult(queryResultTop);
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(50000);
            response.setMessage(e.getMessage());
        } finally {
            return response;
        }
    }
    @ApiOperation("相关性推荐的接口")
    @PostMapping("/more")
    @CrossOrigin
    public Response searchMyFileMoreLikeThis(@RequestBody JSONObject body) {
        String query = body.getString("query");
        Integer from = body.getInteger("from") == null ? 0 : body.getInteger("from");
        Integer size = body.getInteger("size") == null ? 10 : body.getInteger("size");
        Long userId = body.getLong("userId");

        Response response = new Response();

        try {
            QueryResult queryResult = blogService.getMyFilesMoreLikeThis(query, from, size, userId);
            response.setCode(20000);
            response.setMessage("success");
            response.setResult(queryResult);
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(50000);
            response.setMessage(e.getMessage());
        } finally {
            return response;
        }
    }
    @ApiOperation("输入关键字自动补全的接口")
    @GetMapping("/suggest")
    @CrossOrigin
    public Response querySuggest(@ApiParam("输入的关键字") @PathParam("text") String text) {
        Response response = new Response();

        try {
            List<String> suggestions = blogService.getSuggestion(text);
            response.setCode(20000);
            response.setMessage("success");
            response.setResult(suggestions);
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(50000);
            response.setMessage(e.getMessage());
        } finally {
            return response;
        }

    }
}
